package com.irs.service.impl;

import com.irs.mapper.BuildingMusicMapper;
import com.irs.pojo.BuildingMusic;
import com.irs.service.BuildingMusicService;
import com.irs.util.UpUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@Service

@Transactional //事务回滚
public class BuildingMusicServiceImpl implements BuildingMusicService{

    @Resource
    private BuildingMusicMapper buildingMusicMapper
            ;

    public int insert(BuildingMusic pojo){
        return buildingMusicMapper.insert(pojo);
    }

    public int insertList(List< BuildingMusic> pojos){
        return buildingMusicMapper.insertList(pojos);
    }

    public List<BuildingMusic> select(BuildingMusic pojo){
        return buildingMusicMapper.select(pojo);
    }

    public int update(BuildingMusic pojo){
        return buildingMusicMapper.update(pojo);
    }

    //查询
    public List<BuildingMusic> getAll() {
        List<BuildingMusic> musics = buildingMusicMapper.selectAll();
        return musics;
    }

    //删除
    public String del(String[] idlist) {

        for (int k = 0; k < idlist.length; k++) {
            BuildingMusic music = buildingMusicMapper.selectByPrimaryKey(idlist[k]);
            //判断删除的数据是否存在
            if (music!=null){
                buildingMusicMapper.deleteByPrimaryKey(idlist[k]);
            }else {
                return "请刷新页面";
            }
        }
        return "music删除成功";
    }

    //插入

    //更新

    /*public void update(BuildingMusic music) {

        buildingMusicMapper.updateByPrimaryKey(music);
    }*/

    @Override
    //上传下载
    public void up(MultipartFile file, HttpServletRequest request) {
        UpUtils.upfile(file,request);

    }

    @Override
    public void add(BuildingMusic music) {
        //创建id
        String id = UUID.randomUUID().toString();
        music.setId(id);
        //修改url
        String url = music.getUrl();
        String u = "bgm/" + url;
        music.setUrl(u);
        buildingMusicMapper.insert(music);

    }


}
